package com.bwie.parkdemo.mvvm.model;

import android.content.Context;

import com.bwie.lib_core.base.BaseEntity;
import com.bwie.lib_core.mvvm.base.BaseModel;
import com.bwie.lib_core.network.HttpRetrofitFactory;
import com.bwie.lib_core.network.HttpType;
import com.bwie.parkdemo.dao.LoginUserServe;
import com.bwie.parkdemo.mvvm.model.entity.AddSysParkingEntity;
import com.bwie.parkdemo.mvvm.model.entity.CarColorEntity;
import com.bwie.parkdemo.mvvm.model.entity.CarEntity;
import com.bwie.parkdemo.mvvm.model.entity.CarImgEntity;
import com.bwie.parkdemo.mvvm.model.entity.LoginEntity;
import com.bwie.parkdemo.mvvm.model.entity.UploadImgsEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Function;
import io.reactivex.internal.functions.Functions;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 我的车位申请对应model
 *
 * */
public class ApplyParkingModel extends BaseModel {

    private LoginUserServe serve;//获取用户本地缓存数据

    //请求车辆颜色
    @Override
    public Observable<BaseEntity> request(RequestBody... bodyes) {
        Observable<CarColorEntity>source = HttpRetrofitFactory.getInstance()
                .factory(HttpType.TOKENINTERFACE).getRetrofit().create(Api.class)
                .requestCarColor();
        Observable<BaseEntity>ob = Observable.fromArray(source).flatMap((Function) Functions.identity(), false, 1, 1);
        return ob;
    }

    //用户基本信息获取->获取数据库持久化存储的用户信息数据
    //ctx->开关数据库时使用
    public LoginEntity.Values requstUser(Context ctx){
        if (serve == null)
            serve = new LoginUserServe(ctx);
        return serve.selUser();
    }

    //上传图片->多图
    //imgs->图片本地路径地址
    public Observable<UploadImgsEntity> uploadImgs(List<String>imgs){
        List<Observable<UploadImgsEntity>>list = new ArrayList<>();
        //遍历全部本地图片地址->生成对应本地图片被观察者
        for (String path : imgs){
            MultipartBody.Builder builder = new MultipartBody.Builder();
            builder.setType(MultipartBody.FORM);
            RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"),new File(path));
            builder.addFormDataPart("file",path.substring(path.lastIndexOf("/")),body);
            Observable<UploadImgsEntity>ob = HttpRetrofitFactory.getInstance()
                    .factory(HttpType.UPLOAD).getRetrofit().create(Api.class)
                    .uploadIms(builder.build().parts());
            list.add(ob);
        }
        return Observable.merge(list);
    }

    //提交整体用户车辆信息到服务器
    public Observable<CarEntity> requestAddCar(RequestBody body){
        return HttpRetrofitFactory.getInstance().factory(HttpType.TOKENINTERFACE)
                .getRetrofit().create(Api.class).requestAddCar(body);
    }

    //提交车辆图片到服务器
    public Observable<CarImgEntity>requestAddCarImgs(RequestBody body){
        return HttpRetrofitFactory.getInstance().factory(HttpType.TOKENINTERFACE)
                .getRetrofit().create(Api.class).requestCarImgs(body);
    }

    //提交车位信息到服务器
    public Observable<AddSysParkingEntity>requsetAddSysParking(RequestBody body){
        return HttpRetrofitFactory.getInstance().factory(HttpType.TOKENINTERFACE)
                .getRetrofit().create(Api.class).requestAddSysParking(body);
    }

    @Override
    public void cleared() {

    }
}
